package com.sitech.portal.system.action;

import java.io.OutputStream;
import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sitech.BaseConst;
import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.ibnms.util.RandomGUID;
import com.sitech.portal.system.form.SysModuleLogForm;
import com.sitech.portal.system.service.ISysModuleLogService;
import com.sitech.portal.util.excel.ExcelBean;

public class SysModuleLogAction extends JGridBaseAction{

	private ISysModuleLogService sysModuleLogService;
	private String flag;
	private SysModuleLogForm theForm;

	public SysModuleLogForm getTheForm() {
		return theForm;
	}

	public void setTheForm(SysModuleLogForm theForm) {
		this.theForm = theForm;
	}

	public ISysModuleLogService getSysModuleLogService() {
		return sysModuleLogService;
	}

	public void setSysModuleLogService(ISysModuleLogService sysModuleLogService) {
		this.sysModuleLogService = sysModuleLogService;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	public String saveModuleLog(){
		try{
			String moduleId = this.getRequest().getParameter("moduleId");
            String path = this.getRequest().getParameter("path");

            String ip = this.getRequest().getHeader("x-forwarded-for");
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = this.getRequest().getHeader("Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = this.getRequest().getHeader("WL-Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = this.getRequest().getRemoteAddr();
			}
			
			HashMap map = new HashMap();
			map.put("ID", RandomGUID.getRandomGUID());
			map.put("LOGIN_ID", this.getCurrentUser().LOGIN_ID);
			map.put("OPERATE_TIME", new Date());
			map.put("MODULE_ID", moduleId);
			map.put("CLIENT_IP", ip);
            String masterid= (String)this.getRequest().getSession().getAttribute(BaseConst.SESSION_MASTER_USER_KEY);
            map.put("MASTER_ID", masterid);
            map.put("SESSION_4A_ID", this.getRequest().getSession().getId());
            if(path!=null||path.indexOf(":")<0){
                path =path.replace("http://","");
                if(path.indexOf(":")>0){
                    map.put("SERVER_IP", path.split(":")[0]);
                }
            }else{
                InetAddress addr = InetAddress.getLocalHost();
                ip=addr.getHostAddress().toString();
                map.put("SERVER_IP", ip);
            }
            sysModuleLogService.writeSysModuleLog(map);
		}catch(Exception e){
			e.printStackTrace();
		}

		return SUCCESS;
	}
	
	public String loadModuleLogList(){
		return SUCCESS;
	}
	
	public String loadModuleLogListJSON(){
		String startTime = this.getRequest().getParameter("theForm.startTime");
		String endTime = this.getRequest().getParameter("theForm.endTime");
		String moduleId = this.getRequest().getParameter("theForm_moduleId");
		String deptId = this.getRequest().getParameter("theForm_deptId");
		String userId = this.getRequest().getParameter("theForm_userId");
		
		HashMap<String,String> map = new HashMap<String,String>();
		if(startTime!=null&&startTime.length()>0)
			map.put("STARTTIME", startTime);
		if(endTime!=null&&endTime.length()>0)
			map.put("ENDTIME", endTime);
		if(moduleId!=null&&moduleId.length()>0){
			String[] moduleArray = moduleId.split(",");
			String id = "";
			for(int i=0;i<moduleArray.length;i++){
				id += "'"+moduleArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("MODULE_ID", id);
		}
		
		if(userId!=null&&userId.length()>0){
			String[] userArray = userId.split(",");
			String id = "";
			for(int i=0;i<userArray.length;i++){
				id += "'"+userArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("LOGIN_ID", id);
		}else if(deptId!=null&&deptId.length()>0){	//如果选了人就不处理部门
			String[] deptArray = deptId.split(",");
			String id = "";
			for(int i=0;i<deptArray.length;i++){
				id += "'"+deptArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("DEPT_ID", id);
		}
		
		IPage result = sysModuleLogService.findSysModuleLogList(map, this.getPageForm());
		this.setResult(result);
		return SUCCESS;
	}
	
	/**
	 * 模块访问日志汇总
	 * @return
	 */
	public String logModuleGatherReport(){
		return SUCCESS;
	}
	
	public String logModuleGatherReportJSON(){
		String startTime = this.getRequest().getParameter("theForm.startTime");
		String endTime = this.getRequest().getParameter("theForm.endTime");
		String moduleId = this.getRequest().getParameter("theForm_moduleId");
		String deptId = this.getRequest().getParameter("theForm_deptId");
		String userId = this.getRequest().getParameter("theForm_userId");
		
		HashMap<String,String> map = new HashMap<String,String>();
		if(startTime!=null&&startTime.length()>0)
			map.put("STARTTIME", startTime);
		if(endTime!=null&&endTime.length()>0)
			map.put("ENDTIME", endTime);
		if(moduleId!=null&&moduleId.length()>0){
			String[] moduleArray = moduleId.split(",");
			String id = "";
			for(int i=0;i<moduleArray.length;i++){
				id += "'"+moduleArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("MODULE_ID", id);
		}
		
		if(userId!=null&&userId.length()>0){
			String[] userArray = userId.split(",");
			String id = "";
			for(int i=0;i<userArray.length;i++){
				id += "'"+userArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("LOGIN_ID", id);
		}else if(deptId!=null&&deptId.length()>0){	//如果选了人就不处理部门
			String[] deptArray = deptId.split(",");
			String id = "";
			for(int i=0;i<deptArray.length;i++){
				id += "'"+deptArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("DEPT_ID", id);
		}
		
		IPage result = sysModuleLogService.findSysModuleLogGather(map, this.getPageForm());
		this.setResult(result);
		return SUCCESS;
	}
	
	/**
	 * 模块访问分时段统计
	 * @return
	 */
	public String logModuleTimeReport(){
		return SUCCESS;
	}
	public String logModuleTimeReportJSON(){
		String startTime = this.getRequest().getParameter("theForm.startTime");
		String endTime = this.getRequest().getParameter("theForm.endTime");
		String moduleId = this.getRequest().getParameter("theForm_moduleId");
		String deptId = this.getRequest().getParameter("theForm_deptId");
		String userId = this.getRequest().getParameter("theForm_userId");
		
		HashMap<String,String> map = new HashMap<String,String>();
		if(startTime!=null&&startTime.length()>0)
			map.put("STARTTIME", startTime);
		if(endTime!=null&&endTime.length()>0)
			map.put("ENDTIME", endTime);
		if(moduleId!=null&&moduleId.length()>0){
			String[] moduleArray = moduleId.split(",");
			String id = "";
			for(int i=0;i<moduleArray.length;i++){
				id += "'"+moduleArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("MODULE_ID", id);
		}
		
		if(userId!=null&&userId.length()>0){
			String[] userArray = userId.split(",");
			String id = "";
			for(int i=0;i<userArray.length;i++){
				id += "'"+userArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("LOGIN_ID", id);
		}else if(deptId!=null&&deptId.length()>0){	//如果选了人就不处理部门
			String[] deptArray = deptId.split(",");
			String id = "";
			for(int i=0;i<deptArray.length;i++){
				id += "'"+deptArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("DEPT_ID", id);
		}
		
		IPage result = sysModuleLogService.findSysModuleLogTime(map, this.getPageForm());
		this.setResult(result);
		return SUCCESS;
	}
	
	public String exportLogModuleTimeReport(){
		String startTime = this.getRequest().getParameter("theForm.startTime");
		String endTime = this.getRequest().getParameter("theForm.endTime");
		String moduleId = this.getRequest().getParameter("theForm_moduleId");
		String deptId = this.getRequest().getParameter("theForm_deptId");
		String userId = this.getRequest().getParameter("theForm_userId");
		
		HashMap<String,String> map = new HashMap<String,String>();
		if(startTime!=null&&startTime.length()>0)
			map.put("STARTTIME", startTime);
		if(endTime!=null&&endTime.length()>0)
			map.put("ENDTIME", endTime);
		if(moduleId!=null&&moduleId.length()>0){
			String[] moduleArray = moduleId.split(",");
			String id = "";
			for(int i=0;i<moduleArray.length;i++){
				id += "'"+moduleArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("MODULE_ID", id);
		}
		
		if(userId!=null&&userId.length()>0){
			String[] userArray = userId.split(",");
			String id = "";
			for(int i=0;i<userArray.length;i++){
				id += "'"+userArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("LOGIN_ID", id);
		}else if(deptId!=null&&deptId.length()>0){	//如果选了人就不处理部门
			String[] deptArray = deptId.split(",");
			String id = "";
			for(int i=0;i<deptArray.length;i++){
				id += "'"+deptArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("DEPT_ID", id);
		}
		
		List<Map> list = sysModuleLogService.findAllSysModuleLogTime(map);
		
		try {
			String fname = "moduleLogTime";// Excel文件名
			OutputStream os = this.getResponse().getOutputStream();// 取得输出流
			this.getResponse().reset();// 清空输出流
			this.getResponse().setHeader("Content-disposition", "attachment; filename="
					+ fname + ".xls");// 设定输出文件头
			this.getResponse().setContentType("application/msexcel");// 定义输出类型
			ExcelBean eb = new ExcelBean();
			
			String[] columnCNNames = new String[]{"操作时间段","访问次数"};
			String[] keys = new String[]{"OPERATE_TIME","NUM"};
			eb.expordExcel(os, "模块访问时段分布统计", columnCNNames, keys, list);// 调用生成excel文件bean
		} catch (Exception e) {
			System.out.println(e);
		}
		
		return null;
	}
	
	public String exportLogModuleGatherReport(){
		String startTime = this.getRequest().getParameter("theForm.startTime");
		String endTime = this.getRequest().getParameter("theForm.endTime");
		String moduleId = this.getRequest().getParameter("theForm_moduleId");
		String deptId = this.getRequest().getParameter("theForm_deptId");
		String userId = this.getRequest().getParameter("theForm_userId");
		
		HashMap<String,String> map = new HashMap<String,String>();
		if(startTime!=null&&startTime.length()>0)
			map.put("STARTTIME", startTime);
		if(endTime!=null&&endTime.length()>0)
			map.put("ENDTIME", endTime);
		if(moduleId!=null&&moduleId.length()>0){
			String[] moduleArray = moduleId.split(",");
			String id = "";
			for(int i=0;i<moduleArray.length;i++){
				id += "'"+moduleArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("MODULE_ID", id);
		}
		
		if(userId!=null&&userId.length()>0){
			String[] userArray = userId.split(",");
			String id = "";
			for(int i=0;i<userArray.length;i++){
				id += "'"+userArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("LOGIN_ID", id);
		}else if(deptId!=null&&deptId.length()>0){	//如果选了人就不处理部门
			String[] deptArray = deptId.split(",");
			String id = "";
			for(int i=0;i<deptArray.length;i++){
				id += "'"+deptArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("DEPT_ID", id);
		}
		
		List<Map> list = sysModuleLogService.findAllSysModuleLogGather(map);
		
		try {
			String fname = "moduleLogGather";// Excel文件名
			OutputStream os = this.getResponse().getOutputStream();// 取得输出流
			this.getResponse().reset();// 清空输出流
			this.getResponse().setHeader("Content-disposition", "attachment; filename="
					+ fname + ".xls");// 设定输出文件头
			this.getResponse().setContentType("application/msexcel");// 定义输出类型
			ExcelBean eb = new ExcelBean();
			
			String[] columnCNNames = new String[]{"访问模块","点击次数"};
			String[] keys = new String[]{"MODULE_NAME","NUM"};
			eb.expordExcel(os, "模块访问时段分布统计", columnCNNames, keys, list);// 调用生成excel文件bean
		} catch (Exception e) {
			System.out.println(e);
		}
		
		return null;
	}
	
	
	public String exportLogModuleList(){
		String startTime = this.getRequest().getParameter("theForm.startTime");
		String endTime = this.getRequest().getParameter("theForm.endTime");
		String moduleId = this.getRequest().getParameter("theForm_moduleId");
		String deptId = this.getRequest().getParameter("theForm_deptId");
		String userId = this.getRequest().getParameter("theForm_userId");
		
		HashMap<String,String> map = new HashMap<String,String>();
		if(startTime!=null&&startTime.length()>0)
			map.put("STARTTIME", startTime);
		if(endTime!=null&&endTime.length()>0)
			map.put("ENDTIME", endTime);
		if(moduleId!=null&&moduleId.length()>0){
			String[] moduleArray = moduleId.split(",");
			String id = "";
			for(int i=0;i<moduleArray.length;i++){
				id += "'"+moduleArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("MODULE_ID", id);
		}
		
		if(userId!=null&&userId.length()>0){
			String[] userArray = userId.split(",");
			String id = "";
			for(int i=0;i<userArray.length;i++){
				id += "'"+userArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("LOGIN_ID", id);
		}else if(deptId!=null&&deptId.length()>0){	//如果选了人就不处理部门
			String[] deptArray = deptId.split(",");
			String id = "";
			for(int i=0;i<deptArray.length;i++){
				id += "'"+deptArray[i]+"',";
			}
			if(id.length()>0)
				id = id.substring(0,id.length()-1);
			map.put("DEPT_ID", id);
		}
		
		List<Map> list = sysModuleLogService.findAllSysModuleLogList(map);
		
		try {
			String fname = "moduleLog";// Excel文件名
			OutputStream os = this.getResponse().getOutputStream();// 取得输出流
			this.getResponse().reset();// 清空输出流
			this.getResponse().setHeader("Content-disposition", "attachment; filename="
					+ fname + ".xls");// 设定输出文件头
			this.getResponse().setContentType("application/msexcel");// 定义输出类型
			ExcelBean eb = new ExcelBean();
			
			String[] columnCNNames = new String[]{"用户","访问时间","访问模块"};
			String[] keys = new String[]{"NAME","OPERATE_TIME","MODULE_NAME"};
			eb.expordExcel(os, "模块访问日志", columnCNNames, keys, list);// 调用生成excel文件bean
		} catch (Exception e) {
			System.out.println(e);
		}
		
		return null;
	}
}
